$(function(){
	
	$('#datetimepicker12').datetimepicker({
		inline: true,
		sideBySide: true,
		dayViewHeaderFormat: 'YYYY MMMM',
		locale: 'zh-cn',
		format: 'YYYY-MM-DD',
		date:window.location.search
	});
	var date = $('#datetimepicker12').data("DateTimePicker").date().format('YYYY-MM-DD');
	
	var temp = "<tr data-news-id=''><td></td><td></td><td></td><td></td><td style='cursor:pointer' class='td-rqcode' rq-text='' data-toggle='modal'><div coverPic='rqcode'></div></td><td></td><td><a href='' class='btn btn-xs btn-primary' target='_blank'><i class='fa fa-edit'></i></a> <a href='javascript:;' class='btn btn-xs btn-danger news-delete-btn'><i class='fa fa-remove'></i></a></td></tr>";
	$('.modal').modal({
		backdrop: 'static',
		show: false
	});
	$('.btn-box-tool').click(function(){
		$('#add-modal').modal('show');
		$('#search').val("");
	});
	
	//生成二维码
	$('.td-rqcode').each(function(){
		var $this = $(this);
		$this.find('div').qrcode({
			render: "table",
			height: 35,
			width: 35,
			text: $this.attr('rq-text')
		});
	});
	//放大二维码
	$("#newslist").on("click","div[coverpic='rqcode']",function(){
		var bigRQ = $(this).find("table").clone();
		$(bigRQ).css({"width":"240px","height":"240px","margin":"auto"});
		$("#show-rqcode-modal").find(".modal-body").html(bigRQ);
		$("#show-rqcode-modal").modal('show');
	});

	
	$('#datetimepicker12').datetimepicker().on('dp.change',function(ev){
		window.location.href = "/cms/news/daily/list?date="+ev.date.format('YYYY-MM-DD');
	});
	
	
	/* 查询添加  begin*/
	$('.btn-search').click(function(){
		var search = $('#search').val();
		if(search == ""){
			$('#search').tooltip({
				trigger: 'nanual',
				title: '请输入资讯编号或标题'
			}).tooltip('show').one('focus',function(){
				$(this).tooltip('hide');
			});
			return false;
		}
		$('#result table').empty();
		$('#result table').append("正在查询，请稍等");
		cAjax({
			url:"/cms/news/daily/search",
			data:{'search':search},
			success:function(obj){
				$('#result table').empty();
				if(obj.result){
					$.each(obj.data,function(i,n){
						$('#result table').append("<tr><td>"+n.newsId+"</td><td>"+n.title+"</td><td><button type='button' class='btn btn-primary btn-sm add-new'>添加</button></td></tr>");
					});
				}else{
					$('#result table').append("未查询到相关数据");
				}
			},
			error:function(obj){
				alert(obj.msg);
			}
		});
	});
	$("#add-modal").on('click','.add-new',function(){
		var newsId = $(this).parents("tr").find("td:eq(0)").html();
		var valid = true;
		$("#newslist tr").each(function(){
			if($(this).data('news-id')== newsId){
				greeting({msg:"当天不可重复添加相同资讯!"});
				valid = false;
				return;
			}
		});
		if(valid){
			cAjax({
				url:"/cms/news/daily/newsValid",
				data:{"newsId":newsId,"date":date},
				success:function(obj){
					if(obj.data == null){
						save(newsId);
					}else if(obj.data.newsId == 0){
						actionConfirm({	
							msg:"该文章在其他日期发布过，确认添加吗？",
							confirm:function(){
								save(newsId);
							}
						});
					}else{
						greeting({msg:"当天不可重复添加相同资讯!"});
					}
				}
			});
		}
	});
	function save(newsId){
		cAjax({
			url:"/cms/news/daily/add",
			data:{'publishDate':date,newsId:newsId},
			success:function(obj){
					$('#newslist').prepend(temp);
					var $tr = $('#newslist tr:first');
					$tr.attr("data-news-id",obj.data.newsId);
					$tr.find("td:eq(0)").html(obj.data.newsId);
					$tr.find("td:eq(1)").html(obj.data.title);
					$tr.find("td:eq(2)").html(obj.data.authorName);
					$tr.find("td:eq(3)").html("<a href='"+obj.data.shareUrl+"'>"+obj.data.shareUrl+"</a>"); 
					$tr.find("td:eq(4)").attr("re-text",obj.data.shareUrl);
					$tr.find("td:eq(5)").html("未发布");
					$tr.find(".btn-primary").attr("href","/cms/news/edit?newsId="+obj.data.newsId);
					$('#add-modal').modal('hide');
					$tr.find('div').qrcode({
						render: "table",
						height: 35,
						width: 35,
						text: obj.data.shareUrl
					});
			}
		});
	}
	/* 查询添加  end*/
	/* 发布begin */
	$('#publish').on('click','.publish',function(){
		if($('#newslist').children('tr').size()==0){
			greeting({msg:"发布内容不能为空，请添加当日要发布的资讯内容"});
			return;
		}
		var isPublish = $(this).data('ispublish');
		var data = {};
		/*var newsIds = new Array();
		$('#newslist').children('tr').each(function(i,domEle){
			newsIds[i] = $(domEle).attr("data-news-id");
			alert(newsIds[i]);
		});*/
		data['publishDate'] = date;
		data['isPublish'] = isPublish;
		
		var msg = null;
		if(isPublish == 1){
			msg = "是否发布所有资讯";
		}else{
			msg = "是否取消发布所有资讯";
		}
		actionConfirm({
			msg:msg,
			confirm:function(){
				cAjax({
					url:"/cms/news/daily/publish",
					data:data,
					success:function(obj){
						if(isPublish == 1){
							greeting({msg:"成功发布"+obj.data+"篇资讯。"});
						}else{
							greeting({msg:"已经取消"+obj.data+"篇资讯。"});
						}
						window.location.reload();
					}
				});
			}
		});
	});
	/* 发布end */
	
	/* 删除begin */
	$('#newslist').on('click','.news-delete-btn',function(){
		$('#delete-confirm-modal p').html("");
		$('#delete-confirm-modal input[name="newsId"]').val("");
		
		var newsId = $(this).parents("tr").attr("data-news-id");
		$('#delete-confirm-modal p').html("此操作不会删除原资讯,确定要删除编号为"+newsId +"的资讯吗？");
		$('#delete-confirm-modal input[name="newsId"]').val(newsId);
		$('#delete-confirm-modal').modal('show');
	});
	$('#news-del-confirm').click(function(){
		var newsId = $('#delete-confirm-modal input[name="newsId"]').val();
		cAjax({
			url:"/cms/news/daily/del",
			data:{'newsId':newsId,'publishDate':date},
			success:function(obj){
				if(obj.data == 0){
					$("tr[data-news-id='"+newsId+"']").remove();
					$('#delete-confirm-modal').modal('hide');
				}
			}
		});
	});
	/* 删除end */
});
